
package com.android.fitnesscenter.view.client;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.fitnesscenter.R;
import com.android.fitnesscenter.utils.ConfigProperties;
import com.android.fitnesscenter.utils.ConfigProperties.QueryDetailResult;
import com.android.fitnesscenter.utils.ConfigProperties.TradingType;
import com.android.fitnesscenter.utils.Loger;
import com.android.fitnesscenter.utils.MapEntity;
import com.android.fitnesscenter.utils.ViewHelper;
import com.android.fitnesscenter.view.BaseActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;

public class ClientTradingListActivity extends BaseActivity {

    private ArrayList<MapEntity> mMapEntityList;
    private ListView listview;
    private TradingListAdapter mTradingListAdapter;

    @ViewInject(R.id.txt_empty)
    private TextView txt_empty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_layout);
        ViewUtils.inject(this);
        initTitleBar(getString(R.string.txt_pay_list));

        listview = (ListView) findViewById(R.id.listview);
        mMapEntityList = new ArrayList<MapEntity>();
        mTradingListAdapter = new TradingListAdapter();
        listview.setAdapter(mTradingListAdapter);
        queryNewInfo();
    }

    private ProgressDialog mDialog;

    private void queryNewInfo() {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        params.addBodyParameter("token", getToken());
        params.addBodyParameter("imei", getIMEI());
        params.addBodyParameter("type", TradingType.PAY);
        String url = ConfigProperties.SERVICE_URL + "queryNewInfo.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                mDialog = new ProgressDialog(ClientTradingListActivity.this);
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (ClientTradingListActivity.this != null && !ClientTradingListActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        Loger.e("stringResponseInfo.result is " + stringResponseInfo.result);
                        try {
                            JSONObject jsonObject = new JSONObject(stringResponseInfo.result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {

                                } else {
                                    String msg = jsonObject.getString("message");
                                    new ViewHelper(ClientTradingListActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(ClientTradingListActivity.this).showBTNDialog("加载失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Loger.e("msg:" + msg);
                if (ClientTradingListActivity.this != null && !ClientTradingListActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(ClientTradingListActivity.this).showErrorDialog(error);
                }
            }
        });

    }

    private class TradingListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mMapEntityList == null ? 0 : mMapEntityList.size();
        }

        @Override
        public Object getItem(int position) {
            return mMapEntityList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(ClientTradingListActivity.this).inflate(
                        R.layout.query_detail_list_item,
                        null);
                viewHolder = new ViewHolder();
                viewHolder.txt_type = (TextView) convertView.findViewById(R.id.txt_type);
                viewHolder.txt_value = (TextView) convertView.findViewById(R.id.txt_value);
                viewHolder.txt_time = (TextView) convertView.findViewById(R.id.txt_time);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            bindView(viewHolder, position);
            return convertView;
        }

        private void bindView(ViewHolder viewHolder, int position) {
            MapEntity entity = mMapEntityList.get(position);
            String type = entity.getString(QueryDetailResult.TYPE);
            String money = entity.getString(QueryDetailResult.MONEY);
            if (TextUtils.equals(type, TradingType.PAY)) {
                money = "-" + money;
            } else if (TextUtils.equals(type, TradingType.TOP_UP)) {
                money = "+" + money;
            }
            viewHolder.txt_type.setText(type);
            viewHolder.txt_value.setText(money);
            viewHolder.txt_time.setText(entity.getString(QueryDetailResult.CREATE_TIME));

        }

        public class ViewHolder {
            public TextView txt_type;
            public TextView txt_value;
            public TextView txt_time;
        }

    }

    @Override
    protected void titlebarMethod(int id) {

    }

}
